import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import CarView from '@/views/CarView.vue'
import ParklotView from '@/views/ParklotView.vue'
import IndexView from '@/views/IndexView.vue'
import HistoryView from '@/views/HistoryView.vue'
import LoginView from '@/views/LoginView.vue'
import {useTokenStore} from "@/stores/token.js";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: '/login',
      component: LoginView
    }, {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: '/index',
      children: [
        {
          path: '/car',
          component: CarView
        }, {
          path: '/parklot',
          component: ParklotView
        }, {
          path: '/index',
          component: IndexView
        }, {
          path: '/history',
          component: HistoryView
        }
      ]

    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
  ]
});
//导航守卫
router.beforeEach((to, from) => {
  if (to.path == '/login') {
    return true;
  } else {
    const tokenStore = useTokenStore();
    //判断store中有没有token
    if (tokenStore.tokenStr == null) {
      return "/login";
    } else {
      return true;
    }
  }
});

export default router
